package com.neo.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TestService {

    protected static Logger logger= LoggerFactory.getLogger(TestService.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void testTransactional()  {
        String sql= " update `user` set name='test1' where  id = 1  ";
        int res = jdbcTemplate.update(sql);
        int i=1/0;
    }


    @Transactional(rollbackFor = Exception.class)
    public int update(int amount)  {
        Integer age =  jdbcTemplate.queryForObject("select age from `user` where id = 1  ",Integer.class);
        logger.info(Thread.currentThread().getName()+"==========当前数量=========="+age);
        String sql= " update `user` set age = age - ? where age>=? and id = 1  ";
        int res = jdbcTemplate.update(sql,amount,amount);
        logger.info(Thread.currentThread().getName()+"==========更新结果=========="+res);
        if (res==1) {
            try {
                logger.info(Thread.currentThread().getName()+"==========模拟调jx==========");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return res;
    }


}